using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OrbitOne.Elmah.Presenter
{
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    public sealed class ImageCheckBoxList : CheckBoxList
    {
        #region Member variables
        private string _imageUrlPattern;
        private int _imageWidth;
        private int _imageHeight;
        #endregion

        #region Properties
        public string ImageUrlPattern
        {
            get
            {
                if (string.IsNullOrEmpty(_imageUrlPattern))
                {
                    _imageUrlPattern = "{0}";
                }
                return _imageUrlPattern;
            }
            set { _imageUrlPattern = value; }
        }

        public int ImageWidth
        {
            get { return _imageWidth; }
            set { _imageWidth = value; }
        }

        public int ImageHeight
        {
            get { return _imageHeight; }
            set { _imageHeight = value; }
        }
        #endregion

        #region Methods
        protected override void RenderItem(ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
        {
            var item = Items[repeatIndex];
            var image = new Image
                            {
                                    ID = string.Format(item.Value),
                                    ImageUrl = string.Format(_imageUrlPattern, item.Value),
                                    AlternateText = item.Text
                            };
            image.Attributes.Add("title", item.Text);
            if (_imageWidth > 0)
            {
                image.Width = _imageWidth;
            }

            if (_imageHeight > 0)
            {
                image.Height = _imageHeight;
            }

            item.Value = item.Value;
            item.Text = string.Empty;
            base.RenderItem(itemType, repeatIndex, repeatInfo, writer);
            image.RenderControl(writer);
        }
        #endregion
    }
}